package com.example.vehiclemanagesystem.controller;

import com.example.vehiclemanagesystem.entity.Moments;
import com.example.vehiclemanagesystem.entity.User;
import com.example.vehiclemanagesystem.service.Impl.MomentsService;
import com.example.vehiclemanagesystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class MomentsController {

    @Autowired
    private MomentsService momentsServiceImpl;
    @Autowired
    private UserService  userServiceImpl;
    @PostMapping("toMoments")
    public Object toMoments(HttpServletRequest request,@RequestBody Map<String, String> remap){
        String userId =   remap.get("params");
        List<Moments> momentsAll = momentsServiceImpl.getMomentsAll(Long.valueOf(userId));
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        map.put("message", "留言成功！");
        map.put("momentsAll",momentsAll);
        return map;
    }


    @PostMapping("moments/save")
    public Object save(@RequestBody Map<String, String> remap, HttpServletRequest request){
        String userId =   remap.get("userId");
        User user = userServiceImpl.selectUserByUserId(userId);
        String content=remap.get("content");
        Moments moments = new Moments();
        moments.setCreateUserId(Long.valueOf(Long.valueOf(userId)));
        moments.setCreateUserName(user.getName());
        moments.setContent(content);
        int i = momentsServiceImpl.save(moments);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        map.put("message", "留言成功！");
        return map;
    }

    @PostMapping("moments/delete")
    public Object delete(@RequestBody  Map<String, String> remap){
        String id =   remap.get("params");
        int i = momentsServiceImpl.delete(Long.valueOf(id));
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        map.put("message", "删除成功！");
        return map;
    }


}
